using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace PLotSetup
{
    public partial class PLotSetupForm : Form
    {
        public PLotSetupForm() {
            InitializeComponent();
            m_rectList = new List<Rectangle>();
            m_boolPressed = false;
            m_firstPoint = new Point(0, 0);
        }

        private void m_pictureBox_DoubleClick(object sender, EventArgs e) {
            //  MessageBox.Show("Double clicked");
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Multiselect = false;
            ofd.Filter = "bmp files (*.bmp)|*.bmp|Jpeg (*.jpg; *.jpeg)|*.jpg;*.jpeg";
            if (ofd.ShowDialog() == DialogResult.OK) {
                m_label.Visible = false;
                Image img = Image.FromFile(ofd.FileName);
                Size szForm = new Size(img.Size.Width + 40, img.Size.Height + 60);
                Size = szForm;
                m_pictureBox.Height = img.Height;
                m_pictureBox.Width = img.Width;
                m_pictureBox.BackgroundImage = img;
            }
            m_pictureBox.Refresh();
        }

        private void m_pictureBox_Paint(object sender, PaintEventArgs e) {
            Graphics drawObj = e.Graphics;
            Pen pen = new Pen(Color.Black);
            foreach (Rectangle rect in m_rectList)
                drawObj.DrawRectangle(pen, rect);

            if (m_boolPressed) {
                Rectangle rect = new Rectangle();
                rect.X = Math.Min(m_mousePoint.X, m_firstPoint.X);
                rect.Y = Math.Min(m_mousePoint.Y, m_firstPoint.Y);
                Size sz = new Size(Math.Max(m_mousePoint.X, m_firstPoint.X) - rect.X, Math.Max(m_mousePoint.Y, m_firstPoint.Y) - rect.Y);
                rect.Size = sz;
                drawObj.DrawRectangle(pen, rect);
            }

        }


        private void m_pictureBox_MouseMove(object sender, MouseEventArgs e) {
            m_mousePoint = e.Location;
            if (m_boolPressed)
                m_pictureBox.Refresh();
        }

        List<Rectangle> m_rectList;
        bool m_boolPressed;
        Point m_firstPoint;
        Point m_mousePoint;

        private void m_pictureBox_MouseClick(object sender, MouseEventArgs e) {
            if (e.Button == MouseButtons.Right) {
                if (m_boolPressed) {
                    m_boolPressed = false;
                    Rectangle rect = new Rectangle();
                    rect.X = Math.Min(e.X, m_firstPoint.X);
                    rect.Y = Math.Min(e.Y, m_firstPoint.Y);
                    Size sz = new Size(Math.Max(e.X, m_firstPoint.X) - rect.X, Math.Max(e.Y, m_firstPoint.Y) - rect.Y);
                    rect.Size = sz;
                    m_rectList.Add(rect);
                }
                else {
                    m_firstPoint = e.Location;
                    m_boolPressed = true;
                }
            }
        }

        private void Form1_KeyPress(object sender, KeyPressEventArgs e) {
            if (e.KeyChar == 'd' || e.KeyChar == 'D') {
                if (m_rectList.Count > 0) {
                    m_rectList.RemoveAt(m_rectList.Count - 1);
                    m_pictureBox.Refresh();
                }
            }

            if (e.KeyChar == 'O' || e.KeyChar == 'o') {
                StreamWriter sw = File.CreateText("parking.txt");
                foreach (Rectangle rect in m_rectList)
                    sw.WriteLine(rect.X + " " + rect.Y + " " + (rect.Size.Width + rect.X) + " " + (rect.Size.Height + rect.Y));
                sw.Close();

                MessageBox.Show("File saved.", "Save", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
    }
}